/*-------- Includes --------*/
#include <stdio.h>
#include <stdlib.h>

/*-------- Defines --------*/
#define MAX_PAIRS 1000

#define isExceed(i, j) (i < 0 || j < 0 || i == height || j == width)

/*-------- Types --------*/
typedef struct {
    int p;
    int v;
} pixel_t;

/*-------- Global variables --------*/
int width, height;
pixel_t iPix[MAX_PAIRS + 1], oPix[MAX_PAIRS * 9];
int iLen, oLen;

/*-------- Function prototypes --------*/
int cmp(const void *a, const void *b);
int detect(int row, int col, pixel_t *p);
int getValue(int row, int col, pixel_t *p);

/*-------- Main function --------*/
int main(int argc, char const *argv[])
{
    for (scanf("%d", &width); width != 0; scanf("%d", &width)) {
        // read pixels of input picture
        int value, length;
        int index = 0;
        iLen = oLen = 0;
        for (scanf("%d %d", &value, &length); length != 0; scanf("%d %d", &value, &length)) {
            iPix[iLen  ].p = index;
            iPix[iLen++].v = value;
            index += length;
        }
        // sentinel
        iPix[iLen].p = index;
        iPix[iLen].v = -1;
        // height of input picture
        height = index / width;

        // mark leading point and its surrounding points
        for (int k = 0; k < iLen; k++) {
            int row = iPix[k].p / width;
            int col = iPix[k].p % width;

            for (int i = row - 1; i <= row + 1; i++) {
                for (int j = col - 1; j <= col + 1; j++) {
                    // special case: right bound
                    if (j == width && i + 1 < height) {
                        oPix[oLen  ].p = i * width + j;
                        oPix[oLen++].v = detect(i + 1, 0, iPix + k);
                        continue;
                    }

                    // determine whether position is exceeded
                    if (isExceed(i, j)) continue;

                    oPix[oLen  ].p = i * width + j;
                    oPix[oLen++].v = detect(i, j, iPix + k);
                }
            }
        }

        // special case: lower left cornor
        oPix[oLen  ].p = index - width;
        oPix[oLen++].v = detect(height - 1, 0, iPix + iLen);

        // sentinel for oPix
        oPix[oLen].p = index;
        oPix[oLen].v = -1;

        // sort oPix[]
        qsort(oPix, oLen, sizeof(pixel_t), cmp);

        // print iPix[]
        int pos = oPix[0].p;
        int val = oPix[0].v;
        printf("%d\n", width);
        for (int i = 1; i <= oLen; i++) {
            if (pos == oPix[i].p || val == oPix[i].v) continue;
            printf("%d %d\n", val, oPix[i].p - pos);
            pos = oPix[i].p;
            val = oPix[i].v;
        }
        printf("0 0\n");
    }
    printf("0\n");

    return 0;
}

/*-------- Functions --------*/
int cmp(const void *a, const void *b)
{
    const pixel_t *p = a;
    const pixel_t *q = b;

    return p->p - q->p;
}

int detect(int row, int col, pixel_t *p)
{
    int max = 0;
    int val = getValue(row, col, p);

    for (int i = row - 1; i <= row + 1; i++) {
        for (int j = col - 1; j <= col + 1; j++) {
            if ((i == row && j == col) || isExceed(i, j))
                continue;
            int temp = abs(val - getValue(i, j, p));
            if (temp > max) max = temp;
        }
    }

    return max;
}

int getValue(int row, int col, pixel_t *p)
{
    int pos = row * width + col;

    if        (pos <  p->p) {   // find forward
        while (pos <  (--p)->p);
    } else if (pos >= p->p) {   // find backward
        while (pos >= (++p)->p);
        p--;
    }

    return p->v;
}
